package com.yiyibook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyibook.entity.Member;
import com.yiyibook.mapper.MemberMapper;
import com.yiyibook.service.MemberService;
import com.yiyibook.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 〈功能简述〉
 *
 * @author xun.gong@hand-china.com
 * @date 2019/10/14
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member selectByMemberName(String memberName) {
        return memberMapper.selectByMemberName(memberName);
    }

    @Override
    public int insert(Member member) {
        return memberMapper.insertSelective(member);
    }

    @Override
    public Member selectByCondition(Member member) {
        return memberMapper.selectByCondition(member).get(0);
    }

    @Override
    public int updateByCondition(Member member) {
        return memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public ResultEntity<Member> selectAllMember(PageInfo pageInfo, Map<String,String> conditionMap) {
        // 保存查询结果以及分页信息
        ResultEntity<Member> resultEntity = new ResultEntity<>();
        // 当前页
        int pageNum = 1;
        // 页面大小
        int pageSize = 5;
        // 判断是否有分页信息
        if(pageInfo != null){
            // 将新的当前页赋值
            if(pageInfo.getPageNum() != 0){
                pageNum = pageInfo.getPageNum();
            }
            // 将新的页面大小赋值
            if(pageInfo.getPageSize() != 0){
                pageSize = pageInfo.getPageSize();
            }
        }

        // 查询条件
        Member member = new Member();
        member.setMemberRole(null);
        if(conditionMap != null){
            Set<String> keySet = conditionMap.keySet();
            for(String key : keySet){
                if("手机号".equals(key)){
                    member.setMemberPhone(conditionMap.get(key));
                }else if("账号".equals(key)){
                    member.setMemberName(conditionMap.get(key));
                }else if("真实姓名".equals(key)){
                    member.setMemberTrueName(conditionMap.get(key));
                }else if("权限".equals(key)){
                    String memberRole = conditionMap.get(key);
                    if("会员".equals(memberRole)){
                        member.setMemberRole(1);
                    }else if("管理员".equals(memberRole)){
                        member.setMemberRole(0);
                    }
                }
                resultEntity.setSearchTitle(key);
                resultEntity.setKeyWord(conditionMap.get(key));
            }
        }
        // 分页插件
        PageHelper.startPage(pageNum,pageSize);
        // 查询所有的会员信息 取 第 pageNum 页 ，页面大小为 pageSize
        List<Member> members = memberMapper.selectByCondition(member);
        // 获取新的分页信息
        PageInfo<Member> pageInfoNew = new PageInfo<>(members);
        resultEntity.setEntityList(members);
        resultEntity.setPageInfo(pageInfoNew);
        return resultEntity;
    }

    @Override
    public int deleteMemberById(Integer memberId) {
        return memberMapper.deleteByPrimaryKey(memberId);
    }

    @Override
    public int deleteAnyMemberByIds(List<Integer> memberIds) {
        return memberMapper.deleteAnyMemberByIds(memberIds);
    }
}
